package com.kuzlak.cms.libraries;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

    public static String getYmdmdss(Date date) {
        String format = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
        return format;
    }

    public static String getYmdmdss(String str) {
        if (StringUtils.isNotEmpty(str)) {
            Long l = Long.valueOf(str);
            // 创建 Date 对象
            Date date = new Date(l);
            return DateUtil.format(date, "yyyy-MM-dd");
        } else {
            return "";
        }
    }

    public static String getYmdmdss(Date date, int amount) {
        Date date1 = org.apache.commons.lang3.time.DateUtils.addDays(date, amount);

        String format = DateUtil.format(date1, "yyyy-MM-dd HH:mm:ss");
        return format;
    }

    public static Date getDay(Date date, int amount) {
        Date date1 = org.apache.commons.lang3.time.DateUtils.addDays(date, amount);

        return date1;
    }

    public static Date getMonth(Date date, int amount) {
        Date date1 = org.apache.commons.lang3.time.DateUtils.addMonths(date, amount);

        return date1;
    }

    public static Date getYear(Date date, int amount) {
        Date date1 = org.apache.commons.lang3.time.DateUtils.addYears(date, amount);

        return date1;
    }
    //  apply(“date_format(dateColumn,’%Y-%m-%d’) = ‘2008-08-08′”)—>date_format(dateColumn,’%Y-%m-%d’) = ‘2008-08-08′”)

    public static Date getDateType(String date) {
        Date date1 = new Date();
        if (StringUtils.isNotEmpty(date)) {
            try {


                String[] split = date.split("-");
                if (split.length == 3) {

                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    // Parse the string to a Date object
                    date1 = dateFormat.parse(date);
                }
                if (split.length == 2) {

                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
                    // Parse the string to a Date object
                    date1 = dateFormat.parse(date);
                }
                if (split.length == 1) {

                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
                    // Parse the string to a Date object
                    date1 = dateFormat.parse(date);
                }
            } catch (ParseException e) {
                return date1;
            }
        }
        return date1;
    }
}
